

//
//  String+Extension.swift
//  Sina
//
//  Created by YYY on 2017/8/28.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import Foundation

extension String
{
    //匹配正则表达式
    //Swift中，提供了元组，可以同时返回多个值
    //<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">微博 weibo.com</a>
    
    func regularExpression() -> (linkString: String,text: String)?
    {
        //匹配项
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        //创建正则表达式,并进行匹配
        guard let regx = try? NSRegularExpression(pattern: pattern, options: []),
            let result = regx.firstMatch(in: self, options: [], range: NSRange(location: 0, length: characters.count))
            else
        {
            return nil
        }
        let linkString = (self as NSString).substring(with: result.rangeAt(1))
        let text = (self as NSString).substring(with: result.rangeAt(2))
        return (linkString,text)
    }
}
